package com.ce.pms.house.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.common.annotation.AssociatedBuildingQuery;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.house.dao.IGoOutDao;
import com.ce.pms.house.model.GoOutDomain;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;
/**
 * 功能描述: 产权变更DAO实现类
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author  胡煦
 * Create on:2008-8-28
 * Edit author:
 * Edit on:
 * 修改原因：

 */

public class GoOutDaoImpl extends PmsBaseDaoImpl<GoOutDomain> implements
	IGoOutDao {
	/**
	 * 方法描述: 退伙列表页面查询方法,根据传入参数不同执行不同操作，例如：
	 * 1：操作人,查询某个角色操作的全部信息，默认按ID降序排序
	 * 2: 楼栋ID+房号+fitlerStr：精确查询某楼栋，模糊查询房号,而且退伙流程阶段已经处于退伙结束的
	 * 3：房号+业主姓名：根据房号，业主名字，操作人进行模糊查询，默认按ID降序排序
	 * 4：房号+业主姓名+排序参数：根据房号，业主名字进行模糊查询，然后根据order排序
	 * 5：排序参数：查询全部，然后根据order排序
	 * @author 胡煦
	 * Create on:2008-8-28
	 * @param String buildingId,String houseNumber,String owerName,String order,int currentPage,int pageSize
	 * @return Page
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	@AssociatedBuildingQuery(alias="gd",beginStr=" AND ",endStr=" ",property4Building="house.building.keyword",insertAfterPoint="1=1")
	public Page queryGoOutDoListByPage(Long buildingId,String houseNumber,String owerName,String order, int currentPage,int pageSize){
		String buildingIdStr = "gd.house.building.id = ? AND ";
		String houseNumberStr = "gd.house.houseNumber like ? AND ";
		String owerNameStr = "gd.clients like ? AND ";
		String fitlerStr = "gd.goOutStatus.value = 005 AND";
		String orderStr = "order by ?";
		String tolanjieStr = " 1=1 ";
		//String creatorStr = "gd.sysInfo.creator = ? AND";
		String hql = "FROM GoOutDomain gd WHERE :buildingIdStr :houseNumberStr :owerNameStr :fitlerStr :tolanjieStr";
		List<Object> paramList = new ArrayList<Object>();
		int i = 0;

		if(BlankUtil.isBlank(buildingId)) {
			buildingIdStr = "";
			fitlerStr = "";
		}else if(0 == buildingId){
				buildingIdStr = "";
			}else{
				paramList.add(i++, buildingId);
				}
		if (!BlankUtil.isBlank(houseNumber)) {
			paramList.add(i++, "%"+houseNumber+"%");
		}else
			houseNumberStr = "";

		if (!BlankUtil.isBlank(owerName)) {
			paramList.add(i++, "%"+owerName+"%");
		}else
			owerNameStr = "";

		hql = hql.replace(":buildingIdStr ", buildingIdStr).
		replace(":houseNumberStr "  , houseNumberStr)
		 .replace(":owerNameStr " , owerNameStr)
		 //.replace(":creatorStr", creatorStr)
		 .replace(":fitlerStr " , fitlerStr)
		 .replace(":tolanjieStr", tolanjieStr);

		if (hql.endsWith("AND ")) {
			hql = hql.substring(0, hql.lastIndexOf("AND "));
		}

		if (!BlankUtil.isBlank(order)) {
			paramList.add(i++, "gd."+order);
		}else
			orderStr="order by gd.id desc";

		hql = hql + " :orderStr";
		hql = hql.replace(":orderStr" , orderStr);

		Page page = pagedQueryByPageNo(hql, currentPage, pageSize, paramList.toArray());
		return page;
	}

	/**
	 * 方法描述: 检查某房屋是否处于退伙中并返回其状态
	 * @author 胡煦
	 * Create on:2008-9-1
	 * @param houseId
	 * @return SysrefcodeDO:返回不为空说明处于退伙中，否则说明没有处于
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public SysrefcodeDO queryHouseInGoOutBy(Long houseId){
		String hql = "SELECT gd.goOutStatus from GoOutDomain gd where gd.house.id = ? and gd.goOutStatus.value in ('001','002','003','004')";
		Query query = this.createQuery(hql, houseId);
		if(BlankUtil.isBlank(query.list())){
			return null;
		}
		return (SysrefcodeDO) query.list().get(0);
	}

}
